package com.china08.yxyapi.api.manage.tec.office.website.article;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SwsTeaArticleGetController extends YxyApiBaseController {

    @Autowired
    SwsSectionRepository swsSectionRepository;

    @Autowired
    SwsArticleRepository swsArticleRepository;

    @ApiOperation("教师资讯详情")
    @RequestMapping(value = { "/api_yw/manage/teaching/article/get",
            "/api_yw/manage/tec/office/website/article/get" }, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String senderId = getProfile(request, response).getId();
        SwsArticle article = swsArticleRepository.findById(reqModel.id);
        Assert.notNull(article, "资讯不存在");
        Assert.isTrue(StringUtils.equals(senderId, article.getSenderId()), "资讯不存在");
        Assert.isTrue(article.getAuditStatus().intValue() != 1, "审核已通过禁止修改");
        RespModel model = new RespModel();
        model.id = article.getId();
        model.title = article.getTitle();
        model.section1Id = article.getSection1Id();
        model.section2Id = article.getSection2Id();
        model.author = article.getAuthor();
        model.attachmentFileName = article.getAttachmentFileName();
        model.attachmentDownloadUrl = article.getAttachmentDownloadUrl();
        model.content = article.getContent();
        return model;
    }

    public static class ReqModel {
        public String id;
    }

    public static class RespModel {
        public String id;
        public String title;
        public String section1Id;
        public String section2Id;
        public String author;
        public String attachmentFileName;
        public String attachmentDownloadUrl;
        public String content;
    }
}
